package com.example.wanandroid;


import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.M;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager2.widget.ViewPager2;
import androidx.appcompat.widget.Toolbar;

import com.example.wanandroid.core.utils.StatusBarUtils;
import com.example.wanandroid.ui.activity.SearchActivity;
import com.example.wanandroid.ui.adapter.MainViewPagerAdapter;
import com.example.wanandroid.ui.fragment.ArticleListFragment;
import com.example.wanandroid.ui.fragment.NavigationTabsFragment;
import com.example.wanandroid.ui.fragment.ProjectListFragment;
import com.example.wanandroid.ui.fragment.WxArticleFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    ViewPager2 viewPager;
    BottomNavigationView mBottomNavigationView;
    Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initActivity();
        bindViewPagerAndBottomNavBar();
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatusBarUtils.setStatusBar(this);
    }

    private void initActivity(){
        mBottomNavigationView = findViewById(R.id.nav_view);
        viewPager = findViewById(R.id.viewPager);
        mToolbar=findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new ArticleListFragment());
        fragmentList.add(new WxArticleFragment());
        fragmentList.add(new NavigationTabsFragment());
        fragmentList.add(new ProjectListFragment());
        MainViewPagerAdapter mainViewPagerAdapter = new MainViewPagerAdapter(this,fragmentList);
        viewPager.setAdapter(mainViewPagerAdapter);
        viewPager.setUserInputEnabled(false);
    }

    private void bindViewPagerAndBottomNavBar(){
        mBottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.article_list_fragment) {
                    viewPager.setCurrentItem(0);
                    mToolbar.setTitle("首页");
                }  else if (itemId == R.id.tab_navigation) {
                    viewPager.setCurrentItem(2);
                    mToolbar.setTitle("导航");
                } else if (itemId == R.id.project_list_fragment) {
                    viewPager.setCurrentItem(3);
                    mToolbar.setTitle("项目");
                }
                return true;
            }
        });
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        mBottomNavigationView.setSelectedItemId(R.id.article_list_fragment);
                        break;
                    case 1:
                        break;
                    case 2:
                        mBottomNavigationView.setSelectedItemId(R.id.tab_navigation);
                        break;
                    case 3:
                        mBottomNavigationView.setSelectedItemId(R.id.project_list_fragment);
                        break;
                    default:
                        break;
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBottomNavigationView = null;
        viewPager =  null;
        mToolbar = null;
    }
}